package Phase;

import Controllers.AnimationController;
import Entities.Advantages.DecreaseBallSpeed;
import Entities.Advantages.DecreaseBarSize;
import Entities.Advantages.IncreaseBallSpeed;
import Entities.Advantages.IncreaseBarSize;
import Entities.Advantages.ThreeBallsAdvantage;
import Entities.Boundary;
import Entities.Brick;
import Entities.Phase;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FilePhaseLoader implements PhaseLoader {

    ArrayList<Integer> numeroList;
    ArrayList<Integer> brickProperties;
    private Phase phase;
    private String path = "C:/WorldList/";

    @Override
    public Phase loadPhase(int currentWorld, int currentPhase) {
        try {
            path = path + currentWorld + "/fase" + currentPhase + ".txt";

            phase = new Phase();
            Boundary boundary = new Boundary(0, 0, 464, 584);
            phase.addChild(boundary);

            this.fileToPhase();
            if (phase.getNumElems() <= 1) {
                return null;
            }
            phase.setId(currentPhase);
            phase.setWorldId(currentWorld);

            return phase;
        } catch (FileNotFoundException ex) {

            return null;
        }
    }

    @Override
    public void fileToPhase() throws FileNotFoundException {
        try {
            Random r = new Random(System.currentTimeMillis());
            Integer caracter = 0;
            int numero = 0;
            int brickNumber = 0;
            numeroList = new ArrayList();
            brickProperties = new ArrayList();

            try {
                FileReader input = new FileReader(path);
                while (caracter != -1) {

                    caracter = input.read();
                    numero = Character.getNumericValue(caracter);

                    if (numero != -1) {
                        numeroList.add(numero);
                    } else {
                        while (numeroList.size() > 0) {
                            brickNumber = (int) (brickNumber + (numeroList.get(0) * Math.pow((double) 10, (double) numeroList.size() - 1)));
                            numeroList.remove(0);
                        }

                        if (caracter != 10) {
                            brickProperties.add(brickNumber);
                            brickNumber = 0;
                        }
                    }

                    Brick brick = new Brick();
                    if (brickProperties.size() == 3) {
                        brick.setX(brickProperties.get(0));
                        brick.setY(brickProperties.get(1));
                        brick.setResistance(brickProperties.get(2));

                        while (brickProperties.size() > 0) {
                            brickProperties.remove(0);
                        }
                        phase.addChild(brick);
                    }
                    switch (r.nextInt(5)) {
                        case 1: {
                            brick.setAdvantage(new DecreaseBarSize(brick.getX(), brick.getY(), 34, 20));
                            break;
                        }
                        case 2: {
                            brick.setAdvantage(new DecreaseBallSpeed(brick.getX(), brick.getY(), 34, 20));
                            break;
                        }
                        case 3: {
                            brick.setAdvantage(new IncreaseBarSize(brick.getX(), brick.getY(), 34, 20));
                            break;
                        }
                        case 4: {
                            brick.setAdvantage(new ThreeBallsAdvantage(brick.getX(), brick.getY(), 34, 20));
                            break;
                        }
                        default: {
                            brick.setAdvantage(new IncreaseBallSpeed(brick.getX(), brick.getY(), 34, 20));
                            break;
                        }
                    }
                    //brick.setAdvantage(new ThreeBallsAdvantage(brick.getX(), brick.getY(), 34, 20));
                }

            } catch (Exception ex) {
                Logger.getLogger(AnimationController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (Exception e) {
            Logger.getLogger(AnimationController.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public ArrayList<String> loadImagePhaseList(int worldId) {
        String pathWorldId = path + worldId + "/";
        File file = new File(pathWorldId);
        ArrayList<String> imagePhaseList = new ArrayList<String>();
        String pathPhaseImage = "/Images/phase.png";
        for (int i = 0; i < file.list().length - 1; i++) {
            imagePhaseList.add(pathPhaseImage);
        }
        return imagePhaseList;
    }
}
